package com.jianyun.jyzs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.adapter.EngLocAdapter;
import com.jianyun.jyzs.bean.EngProjectLocResult;
import com.jianyun.jyzs.common.ITextWatchListener;
import com.jianyun.jyzs.constant.SysConstant;
import com.jianyun.jyzs.dao.EngLocationDao;
import com.jianyun.jyzs.http.Api;
import com.jianyun.jyzs.http.CustomCallback;
import com.jianyun.jyzs.http.RetrofitHelper;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.utils.UtilsStyle;
import com.jianyun.jyzs.widget.LoadingDialog;
import com.jrmf360.rylib.common.util.ToastUtil;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 创建工程动态-->工程部位选择
 * Created by yangshijun on 2018/5/31.
 */

public class EngineerLocationActivity extends AppCompatActivity {

    @BindView(R.id.topBack)
    ImageView topBack;
    @BindView(R.id.topText)
    TextView topText;
    @BindView(R.id.tv_checked)
    TextView tvChecked;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.iv_moreLinearLayout)
    LinearLayout ivMoreLinearLayout;
    @BindView(R.id.topCreat)
    LinearLayout topCreat;
    @BindView(R.id.iv_single)
    ImageView ivSingle;
    @BindView(R.id.iv_group)
    ImageView ivGroup;
    @BindView(R.id.topChatDetail)
    LinearLayout topChatDetail;
    @BindView(R.id.topId)
    RelativeLayout topId;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private String projectid;
    private HashMap<String, String> map;
    private EngLocationDao dao;
    private List<EngProjectLocResult.EngTypeLocBean> engineer;
    private EngLocAdapter adapter;
    private LoadingDialog dialog;
    private String enterpriseCode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        UtilsStyle.setStatusBarMode(this,true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_recyview);
        ButterKnife.bind(this);
        initData();
        initView();
    }

    private void initData() {
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
        dao = new EngLocationDao(this);
        projectid = getIntent().getStringExtra(SysConstant.msg_key_string1);
        map = new HashMap<>();
        map.put("method", "GetProJectPostionList");
        map.put("ProJectId", projectid);
        map.put("enterpriseCode", enterpriseCode);
        map.put("appCode", "jyzs");
        map.put("appType", "Android");
        map.put("appVersion", "1.0.2");
    }

    private void initView() {
        topBack.setVisibility(View.VISIBLE);
        topText.setVisibility(View.VISIBLE);
        topText.setText("选择部位");
        tvChecked.setVisibility(View.VISIBLE);
        tvChecked.setText("刷新");
        tvChecked.setTextColor(getResources().getColor(R.color.blue));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new EngLocAdapter(this, null);
        recyclerView.setAdapter(adapter);
        dialog = LoadingDialog.create(this).setCancellable(false);
        adapter.setResultListener(new EngLocAdapter.ResultListener() {
            @Override
            public void setMsg(String msg) {
                setResult(-1,new Intent().putExtra(SysConstant.msg_key_string1,msg));
                finish();
            }
        });
        updateDate();
        setEngTypeView(null);
        etSearch.addTextChangedListener(new ITextWatchListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                setEngTypeView(str);
            }
        });
    }

    @OnClick({R.id.topBack, R.id.tv_checked})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.topBack:
                finish();
                break;
            case R.id.tv_checked:
                updateDate();
                break;
        }
    }

    private void setEngTypeView(String str) {
        if (str == null) {
            engineer = dao.getEngineer(projectid);
        } else {
            engineer = dao.getEngineerSearch(str, projectid);
        }

        adapter.setList(engineer);
    }

    private void updateDate() {
        dialog.show();
        Api api = RetrofitHelper.builderCeshiHttp().create(Api.class);
        api.getNewEngineerLoc(map).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CustomCallback<EngProjectLocResult>() {
                    @Override
                    protected void onResponse(EngProjectLocResult response) {
                        dialog.dismiss();
                        if (response.isResult()) {
                            dao.delAll();
                            List<EngProjectLocResult.EngTypeLocBean> list = response.getList();
                            for (EngProjectLocResult.EngTypeLocBean ee : list) {
                                dao.insert(ee);
                            }
                            setEngTypeView(null);

                        } else {
                            ToastUtil.showNoWaitToast(EngineerLocationActivity.this, response.getMessage());
                        }
                    }

                    @Override
                    protected void onException(String msg) {
                        dialog.dismiss();
                        ToastUtil.showNoWaitToast(EngineerLocationActivity.this, msg);
                    }
                });
    }
}
